package com.yt.system.controller;

import com.yt.system.domain.GroupAccount;
import com.yt.system.domain.GroupHistory;
import com.yt.system.domain.Week;
import com.yt.system.service.GroupAccountService;
import com.yt.system.service.GroupHistoryService;
import com.yt.system.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
@Api(tags = "小组加减分记录管理")
@RestController
public class GroupHistoryController {
    @Autowired
    GroupHistoryService groupHistoryService;
    @Autowired
    GroupAccountService groupAccountService;

    /**
     *查询所有小组的历史积分记录
     * @return
     */
    @ApiOperation(value = "查询所有小组的历史积分记录")
    @GetMapping("/getAllGroupHistory")
    public List<GroupHistory> getAllGroupHistory(){
        return groupHistoryService.selectAllGroupHistory();
    }

    /**
     * 根据小组ID查询本周加减分记录
     * @param gid
     * @return
     */
    @ApiOperation(value = "根据小组ID查询本周加减分记录")
    @GetMapping("/getWeekGroupHistoryByGid/{gid}")
    public List<GroupHistory> getWeekGroupHistoryByGid(@PathVariable("gid") Integer gid){
        TimeUtil timeUtil = new TimeUtil();
        Map<String, Date> map = timeUtil.getMondayAndCurday();
        List<GroupHistory> list = new ArrayList<>();
        List<GroupHistory> groupHistories = groupHistoryService.selectWeekGroupHistory(map);
        for(GroupHistory groupHistory:groupHistories){
            if(groupHistory.getGid().equals(gid)){
                list.add(groupHistory);
            }
        }
        return list;
    }


    /**
     *根据小组id查询小组历史积分记录
     * @param gid
     * @return
     */
    @ApiOperation(value = "根据小组id查询小组历史积分记录")
    @GetMapping("/selectByGid/{gid}")
    public List<GroupHistory> selectByGid(@PathVariable("gid") Integer gid){
       return groupHistoryService.selectByGid(gid);
    }

    /**
     *插入小组历史积分记录
     * @param groupHistory
     * @return
     */
    @ApiOperation(value = "插入小组历史积分记录")
    @PostMapping("insertGroupHistory")
    public String insertGroupHistory(GroupHistory groupHistory){
        //获取小组当前的积分
        Double score = groupAccountService.selectByGid(groupHistory.getGid()).getGscore();
        //获取加减分字符串
        Double operatescore = groupHistory.getOperatesorce();
        score+=operatescore;
        groupHistory.setTotalchange(score);
        try{
            groupHistoryService.insertGroupHistory(groupHistory);
            GroupAccount groupAccount = new GroupAccount();
            groupAccount.setGid(groupHistory.getGid());
            groupAccount.setGscore(score);
            groupAccountService.updateByGidSelective(groupAccount);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 查询本周一到周日小组加分情况
     * @param gid
     * @return
     */
    @ApiOperation(value = "查询本周一到周日小组加分情况")
    @GetMapping("/getPerDateGroupScoreByGid/{gid}")
    public Week getPerDateScoreByAid(@PathVariable("gid") Integer gid){
        //获取当前日期和本周一的日期
        TimeUtil timeUtil = new TimeUtil();
        Map<String, Date> map = timeUtil.getMondayAndCurday();
        //设置时间格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String monday = sdf.format(map.get("monday"));
        String tuesday = sdf.format(map.get("tuesday"));
        String wensday = sdf.format(map.get("wensday"));
        String thursday = sdf.format(map.get("thursday"));
        String friday = sdf.format(map.get("friday"));
        String saturday = sdf.format(map.get("saturday"));
        String sunday = sdf.format(map.get("sunday"));
        //根据周一和当前日查询出所有加减分记录
        List<GroupHistory> list = groupHistoryService.selectByGid(gid);
        Week week = new Week(0.0,0.0,0.0,0.0,0.0,0.0,0.0);
        for (GroupHistory groupHistory:list){
            Double fs = groupHistory.getOperatesorce();
            if(sdf.format(groupHistory.getOperatetime()).equals(monday)){
                week.setMonday(week.getMonday()+fs);
            }else if(sdf.format(groupHistory.getOperatetime()).equals(tuesday)){
                week.setTuesday(week.getTuesday()+fs);
            }else if(sdf.format(groupHistory.getOperatetime()).equals(wensday)){
                week.setWensday(week.getWensday()+fs);
            }else if(sdf.format(groupHistory.getOperatetime()).equals(thursday)){
                week.setThursday(week.getThursday()+fs);
            }else if(sdf.format(groupHistory.getOperatetime()).equals(friday)){
                week.setFriday(week.getFriday()+fs);
            }else if(sdf.format(groupHistory.getOperatetime()).equals(saturday)){
                week.setSaturday(week.getSaturday()+fs);
            }else if(sdf.format(groupHistory.getOperatetime()).equals(sunday)){
                week.setSunday(week.getSunday()+fs);
            }
        }
        return week;
    }
}
